package cn.Robbery.basic.collection;

import cn.Robbery.basic.util.AjaxResult;
import cn.Robbery.basic.util.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true) MultipartFile file){

        try {
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".")+1);
            String path = FastDfsUtil.upload(file.getBytes(),extName);
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("上传失败 "+e.getMessage());
        }
    }

    @DeleteMapping
    public AjaxResult del(@RequestParam(value = "path") String path){

        try {
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);
            FastDfsUtil.delete(groupName, remotePath);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败"+e.getMessage());
        }
    }
}
